<?php
	require_once './dir.func.php';
	require_once './file.func.php';
	require_once './common.func.php';
	date_default_timezone_set("PRC");
	$path = "./file";
	$path2 = $path;
	if(isset($_REQUEST['path'])){
		$path = $_REQUEST['path']?$_REQUEST['path']:$path;
		$path2 = $path;
		$path = mb_convert_encoding($path,'GBK');
	}
	$info = readDirectory($path,$path2);
	if(empty($info['file'])&&empty($info['dir'])){
		echo "<script type='text/javascript'>alert('没有文件和目录');location.href='./index.php';</script>";
	}
	$redirect = "./index.php?path={$path2}";
	if(isset($_REQUEST['act'])){
		$filename = $_REQUEST['filename'];
		$act = $_REQUEST['act'];
		$dirname = $_REQUEST['dirname'];
		if($act == 'createFile'){
		//创建文件
		$mes = createFile($path."/".$filename);
		alertMes($mes,$redirect);
		}
		elseif($act=='showContent') {
			//查看文件内容
			$filename = mb_convert_encoding($filename,"GBK");
			$content = file_get_contents($filename);
			if(strlen($content)){
				$newContent = highlight_string($content,true);
				echo $newContent;
			}
			else{
				alertMes("文件内容为空，请编辑",$redirect);
			}
		}
		elseif ($act=='editContent') {
			//编辑文件内容
			$filename1 = $filename;
			$filename = mb_convert_encoding($filename,"GBK");
			$content = file_get_contents($filename);
			$str = <<< EOF
			<form action="index.php?act=doEdit&filename=''" method='post'>
				<textarea name='content' cols='190' rows='10'>
					{$content}
				</textarea><br>
				<input type='hidden' name='filename' value='{$filename1}'>
				<input type='hidden' name='path' value='{$path2}'>
				<input type='hidden' name='dirname' value='{$path2}'>
				<input type='submit' value='修改文件'>
			</form>
EOF;
			echo $str;
		}
		elseif ($act=='doEdit') {
			//修改文件内容
			$content = $_REQUEST['content'];
			$filename = $_REQUEST['filename'];
			$filename = mb_convert_encoding($filename,'GBK');
			if(file_put_contents($filename,$content)){
				$mes = '文件修改成功';
			}
			else{
				$mes = '文件修改失败';
			}
			alertMes($mes,$redirect);
		}
		elseif ($act=='renameFile') {
			//完成文件重命名
			$filname = $_REQUEST['filename'];
			$str =<<< EOF
			<form action="index.php?act=doRename&filename={$filename}" method='post'>
				请填写新文件名:<input type='text' name='newname' placeholder='重命名'>
				<input type='hidden' name='dirname' value='{$path2}'>
				<input type='hidden' name='path' value='{$path2}'>
				<input type='submit' value='重命名'>
			</form>
EOF;
			echo $str;
		}
		elseif ($act=='doRename') {
			//实现文件的重命名
			$newname = $_REQUEST['newname'];
			$mes = renameFile($filename,$newname);
			alertMes($mes,$redirect);
		}
		elseif ($act=='deleteFile'){
			//文件删除
			$mes = delFile($filename);
			alertMes($mes,$redirect);
		}
		elseif ($act=='downloadFile') {
			//下载文件
			$filename = $_REQUEST['filename'];
			$mes = downFile($filename);
		}
		elseif ($act=='createFolder'){
			$mes = createFolder($path2.'/'.$dirname);
			alertMes($mes,$redirect);
		}
		elseif ($act=='renameFolder') {
			//重命名文件夹
			$str =<<< EOF
			<form action="index.php?act=doRenameFolder&filename={$filename}&dirname={$dirname}&path={$path2}" method='post'>
				请填写新文件夹名:<input type='text' name='newname' placeholder='重命名'>
				<input type='submit' value='重命名'>
			</form>
EOF;
			echo $str;
		}
		elseif ($act=='doRenameFolder') {
			//实现文件夹重命名
			$newname = $_REQUEST['newname'];
			$mes = renameFolder($dirname,$path2.'/'.$newname);
			alertMes($mes,$redirect);
		}
		elseif ($act=='copyFolder') {
			//拷贝文件夹
			$str =<<< EOF
			<form action="index.php?act=doCopyFolder&filename={$filename}&dirname={$dirname}&path={$path2}" method='post'>
				将文件夹复制到:<input type='text' name='dstname' placeholder='将文件夹复制到'>
				<input type='submit' value='复制文件夹'>
			</form>
EOF;
			echo $str;
		}
		elseif ($act=='doCopyFolder'){
			$dstname = $_REQUEST['dstname'];
			$stack = explode('/',$filename);
			$basename = array_pop($stack);
			$mes = copyFolder($dirname,$path2.'/'.$dstname.'/'.$basename);
			alertMes($mes,$redirect);
		}
		elseif ($act=='cutFolder'){
			//剪切文件夹
			$str =<<< EOF
			<form action="index.php?act=doCutFolder&filename={$filename}&dirname={$dirname}&path={$path2}" method='post'>
				将文件夹剪切到:<input type='text' name='dstname' placeholder='将文件夹剪切到'>
				<input type='submit' value='剪切文件夹'>
			</form>
EOF;
			echo $str;
		}
		elseif ($act=='doCutFolder'){
			$dstname = $_REQUEST['dstname'];
			$mes = cutFolder($dirname,$path2.'/'.$dstname);
			alertMes($mes,$redirect);
		}
		elseif ($act=='deleteFolder'){
			//完成文件夹删除
			$mes = delFolder($dirname);
			alertMes($mes,$redirect);
		}
		elseif ($act=='copyFile'){
			//文件复制
			$str =<<< EOF
			<form action="index.php?act=doCopyFile&filename={$filename}&dirname={$filename}&path={$path2}" method='post'>
				将文件复制到:<input type='text' name='dstname' placeholder='将文件复制到'>
				<input type='submit' value='复制文件'>
			</form>
EOF;
			echo $str;
		}
		elseif ($act=='doCopyFile'){
			$dstname = $_REQUEST['dstname'];
			$mes = copyFile($filename,$path2.'/'.$dstname);
			alertMes($mes,$redirect);
		}
		elseif ($act=='cutFile'){
			//文件复制
			$str =<<< EOF
			<form action="index.php?act=doCutFile&filename={$filename}&dirname={$filename}&path={$path2}" method='post'>
				将文件剪切到:<input type='text' name='dstname' placeholder='将文件剪切到'>
				<input type='submit' value='剪切文件'>
			</form>
EOF;
			echo $str;
		}
		elseif ($act=='doCutFile'){
			$dstname = $_REQUEST['dstname'];
			$mes = cutFile($filename,$path2.'/'.$dstname);
			alertMes($mes,$redirect);
		}
		elseif ($act=='上传文件'){
			$fileInfo = $_FILES['myFile'];
			$mes = uploadFile($fileInfo,$path2);
			alertMes($mes,$redirect);
		}
	}
?>

<!DOCTYPE html>
<html>
	<head>
		<meta content="text/html" charset="UTF-8">
		<link rel="stylesheet" type="text/css" href="./cikonss.css">
		<link rel="stylesheet" type="text/css" href="./basestyle.css">
		<link rel="stylesheet" type="text/css" href="./jquery_ui/css/jquery-ui.min.css">
		<script type="text/javascript" src="./basefun.js"></script>
		<script type="text/javascript" src='./jquery_ui/external/jquery/jquery.js'></script>
		<script type="text/javascript" src='./jquery_ui/js/jquery-ui.min.js'></script>
	</head>
	<body>
		<header>
			<div id='showDetail' style='display:none'><img src="" id='showImg' alt=''></div>
			<h1>在线文件管理器</h1>
			<nav>
				<ul class="menu">
					<li><span class="icon icon-small icon-square"><a href="./index.php" title="主页"><span class="icon-home"></span></a></span></li>
					<li><span class="icon icon-small icon-square"><a href="#" onclick="show('createFile')" title="新建文件"><span class="icon-file"></span></a></span></li>
					<li><span class="icon icon-small icon-square"><a href="#" onclick="show('createFolder')" title="新建文件夹"><span class="icon-folder"></span></a></span></li>
					<li><span class="icon icon-small icon-square"><a href="#" onclick="show('uploadFile')" title="上传文件"><span class="icon-upload"></span></a></span></li>
					<?php 
						$back = $path == "./file"?"./file":dirname($path2);
					 ?>
					<li><span class="icon icon-small icon-square"><a href="#" title="向上" onclick="goBack('<?php echo $back; ?>')"><span class="icon-arrowLeft"></span></a></span></li>
				</ul>
				<div class="clearfloat"></div>
			</nav>
			<section>
				<form action="index.php" method="post" enctype="multipart/form-data">
				<table class="list">
					<tr id="createFile" style="display: none;">
						<td>请输入创建的文件名</td>
						<td>
							<input type="text" name="filename">
							<input type="hidden" name="path" value="<?php echo $path2;?>">
							<input type="hidden" name="act" value="createFile">
							<input type='hidden' name='dirname' value='<?php echo $path2; ?>'>
							<input type="submit" value="创建文件">
						</td>
					</tr>
					<tr id='createFolder' style='display:none'>
						<td>请输入创建的文件夹名</td>
						<td>
							<input type="text" name="dirname">
							<input type="hidden" name="path" value="<?php echo $path2;?>">
							<input type="hidden" name="act" value="createFolder">
							<input type='hidden' name='filename' value='wodeai'>
							<input type="submit" value="创建文件夹">
						</td>
					</tr>
					<tr id='uploadFile' style='display:none'>
						<td>请选择要上传的文件</td>
						<td>
							<input type='file' name='myFile'>
							<input type='submit' name='act' value='上传文件'>
						</td>
					</tr>
					<tr>
						<td>编号</td>
						<td>名称</td>
						<td>类型</td>
						<td>大小</td>
						<td>可读</td>
						<td>可写</td>
						<td>可执行</td>
						<td>创建时间</td>
						<td>修改时间</td>
						<td>访问时间</td>
						<td>操作</td>

					<!-- 读取文件 -->
					</tr>
					<?php
						if($info['file']){
								$i = 1;
								foreach ($info['file'] as $value) {
									$p = $path2."/".$value;
									$p1 = $p;
					?>				
									<tr>
										<td><?php echo $i;?></td>
										<td><?php echo $value;?></td>
										<td>
										  <?php $p = mb_convert_encoding($p, "GBK");$src = filetype($p)=="file"?"file_ico.png":"folder_ico.png"; ?>
										<img src="./images/<?php echo $src; ?>" alt="" title="文件">
										</td>
										<td><?php echo transByte(filesize($p));?></td>
										<td>
										  <?php  $src = is_readable($p)?"correct.png":"error.png"; ?>
										  <img alt="" src="./images/<?php echo $src;?>">
										</td>
										<td>
										  <?php  $src = is_writable($p)?"correct.png":"error.png"; ?>
										  <img alt="" src="./images/<?php echo $src;?>">
										</td>
										<td>
										  <?php  $src = is_executable($p)?"correct.png":"error.png"; ?>
										  <img alt="" src="./images/<?php echo $src;?>">
										 </td>
										 <td>
										  <?php  echo date("Y-m-d H:i:s", filectime($p));?>
										 </td>
										 <td>
										  <?php  echo date("Y-m-d H:i:s", filemtime($p));?>
										 </td>
										 <td>
										  <?php  echo date("Y-m-d H:i:s", fileatime($p));?>
										 </td>
										 <td>
										 	<!-- 获得文件扩展名 -->
											<?php
												$explodestr = explode('.',$value);
												$endstr = end($explodestr);
												$ext = strtolower($endstr);
												$extImg = array('gif','jpg','jpeg','png','bmp');
												if(in_array($ext,$extImg)){
											?>
													<a href="#" onclick="showImage('<?php echo $value;?>','<?php echo $p1;?>')"><img src="./images/show.png" alt="" title="查看"></a>
											<?php
												}
												else{
											?>
										 	<a href="index.php?act=showContent&path=<?php echo $path2; ?>&dirname=<?php echo $path2; ?>&filename=<?php echo $p1;?>"><img src="./images/show.png" alt="" title="查看"></a>
										 	<?php } ?>
										 	<a href="index.php?act=editContent&path=<?php echo $path2; ?>&dirname=<?php echo $path2; ?>&filename=<?php echo $p1;?>"><img src="./images/edit.png" alt="" title="修改"></a>
										 	<a href="index.php?act=renameFile&path=<?php echo $path2; ?>&dirname=<?php echo $path2; ?>&filename=<?php echo $p1; ?>"><img src="./images/rename.png" alt="" title="重命名"></a>
										 	<a href="index.php?act=copyFile&path=<?php echo $path2; ?>&dirname=<?php echo $path2; ?>&filename=<?php echo $p1; ?>"><img src="./images/copy.png" alt="" title="复制"></a>
										 	<a href="index.php?act=cutFile&path=<?php echo $path2; ?>&dirname=<?php echo $path2; ?>&filename=<?php echo $p1; ?>"><img src="./images/cut.png" alt="" title="剪切"></a>
										 	<a href="#" onclick="deleteFile('<?php echo $p1; ?>','<?php echo $path2; ?>')"><img src="./images/delete.png" alt="" title="删除"></a>
										 	<a href="index.php?act=downloadFile&path=<?php echo $path2; ?>&dirname=<?php echo $path2; ?>&filename=<?php echo $p1; ?>"><img src="./images/download.png" alt="" title="下载"></a>
										 </td>
									</tr>
					<?php
								$i++;
								}
						}
					?>

					<!-- 读取文件夹 -->
					</tr>
					<?php
						if($info['dir']){
								if(!isset($i)){
									$i = 1;
								}
								foreach ($info['dir'] as $value) {
									$p = $path2."/".$value;
									$p1 = $p;
					?>				
									<tr>
										<td><?php echo $i;?></td>
										<td><?php echo $value;?></td>
										<td>
										  <?php $p = mb_convert_encoding($p, "GBK");$src = filetype($p)=="file"?"file_ico.png":"folder_ico.png"; ?>
										<img src="./images/<?php echo $src; ?>" alt="" title="文件">
										</td>
										<td><?php $sum = 0; echo transByte(dirSize($p));?></td>
										<td>
										  <?php  $src = is_readable($p)?"correct.png":"error.png"; ?>
										  <img alt="" src="./images/<?php echo $src;?>">
										</td>
										<td>
										  <?php  $src = is_writable($p)?"correct.png":"error.png"; ?>
										  <img alt="" src="./images/<?php echo $src;?>">
										</td>
										<td>
										  <?php  $src = is_executable($p)?"correct.png":"error.png"; ?>
										  <img alt="" src="./images/<?php echo $src;?>">
										 </td>
										 <td>
										  <?php  echo date("Y-m-d H:i:s", filectime($p));?>
										 </td>
										 <td>
										  <?php  echo date("Y-m-d H:i:s", filemtime($p));?>
										 </td>
										 <td>
										  <?php  echo date("Y-m-d H:i:s", fileatime($p));?>
										 </td>
										 <td>
										 	<a href="index.php?path=<?php echo $p1;?>"><img src="./images/show.png" alt="" title="查看"></a>
										 	<a href="index.php?act=renameFolder&path=<?php echo $path2; ?>&dirname=<?php echo $p1; ?>&filename=<?php echo $p1; ?>&path=<?php echo $path2; ?>"><img src="./images/rename.png" alt="" title="重命名"></a>
										 	<a href="index.php?act=copyFolder&path=<?php echo $path2; ?>&dirname=<?php echo $p1; ?>&filename=<?php echo $p1; ?>"><img src="./images/copy.png" alt="" title="复制"></a>
										 	<a href="index.php?act=cutFolder&path=<?php echo $path2; ?>&dirname=<?php echo $p1; ?>&filename=<?php echo $p1; ?>"><img src="./images/cut.png" alt="" title="剪切"></a>
										 	<a href="#" onclick="deleteFolder('<?php echo $p1; ?>','<?php echo $path2 ?>')"><img src="./images/delete.png" alt="" title="删除"></a>
										 	<a href="index.php?act=downloadFile&path=<?php echo $path2; ?>&filename=<?php echo $p1; ?>"><img src="./images/download.png" alt="" title="下载"></a>
										 </td>
									</tr>
					<?php
								$i++;
								}
							}
					?>
				</table>
				</form>
			</section>
		</header>
	</body>
</html>

